package com.ml.doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ml.doctor.CustomApplication;
import com.ml.doctor.R;
import com.ml.doctor.bean.LoginBean;
import com.ml.doctor.call2.NimAccountHelper;
import com.ml.doctor.call2.QianZui;
import com.ml.doctor.dialog.LoadingDialog;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.utils.ToastUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private static String TAG = "LoginActivity";
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.register)
    TextView register;
    @BindView(R.id.login)
    TextView login;

    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setTopTitle("登录");
    }


    /**
     * 用户登录
     */
    private void login() {
        String user = name.getText().toString().trim();
        String pwd = password.getText().toString().trim();
        if (TextUtils.isEmpty(user)) {
            ToastUtil.showShort(LoginActivity.this, getString(R.string.name_tip));
            return;
        }
//        if (!isPhone(user)) {
//            ToastUtil.showShort(LoginActivity.this, "请输入手机号");
//            return;
//        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.showShort(LoginActivity.this, getString(R.string.pwd_tip));
            return;
        }
        //显示加载dialog
        showLoadingDialog("正在登陆");
        NetworkApi.login(user, pwd, new NetworkManager.SuccessCallback<LoginBean>() {
            @Override
            public void onSuccess(LoginBean response) {
                startActivity(new Intent(LoginActivity.this, MainActivity.class));//跳转到列表界面
                NimAccountHelper.getInstance().login(QianZui.qianZuiDoc + CustomApplication.getInstance().userId, "123456",null);
                ToastUtil.showShort(LoginActivity.this, getString(R.string.login_success));
                Log.e(TAG, response.toString());
                saveToLocal(response);
                finish();
                hideLoadingDialog();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(LoginActivity.this, message);
                hideLoadingDialog();
            }
        });

    }

    /**
     * 保存一些基本信心到SP中
     */
    private void saveToLocal(LoginBean response) {
        CustomApplication.getInstance().userId = response.getDocterid();
        LocalShared.getInstance(this).setUserId(response.getDocterid());
        LocalShared.getInstance(this).setUserNick(response.getDoctername());
        LocalShared.getInstance(this).setUserPhone(response.getTel());
        LocalShared.getInstance(this).setUserPro(response.getPro());
        LocalShared.getInstance(this).setPhoto(response.getDocter_photo());
    }

    public void showLoadingDialog(String message) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this, message);
        }
        mLoadingDialog.show();
    }

    public void hideLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @OnClick({R.id.register, R.id.login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register:
                startActivity(new Intent(this, ResActivity.class));
                break;
            case R.id.login:
                login();
                break;
        }
    }

    public boolean isPhone(String strPhone) {
        String strPattern = "^((?:13\\d|14[\\d]|15[\\d]|17[\\d]|18[\\d]|19[\\d]|12[\\d])-?\\d{5}(\\d{3}|\\*{3}))$";

        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strPhone);
        return m.matches();
    }
}
